package com.cheyun.netsalev3.repository

import androidx.paging.PageKeyedDataSource
import com.cheyun.netsalev3.bean.FollowParam
import com.cheyun.netsalev3.bean.drive.DriveListInfo
import com.cheyun.netsalev3.bean.drive.DriveListParam
import com.cheyun.netsalev3.bean.drive.Reservemit
import com.cheyun.netsalev3.utils.api.*
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers


class DriveListPageRepository(var state: Int) :BasePageRepository<DriveListInfo>() {

    override fun loadInitial(params: PageKeyedDataSource.LoadInitialParams<Int>, callback: (data: MutableList<DriveListInfo>) -> Unit) {
        ApiClient.getInstance().getDriveList(1, state)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<DriveListParam>>() {
                    override fun onSuccess(t: BaseResp<DriveListParam>) {
                        t.data?.rows?.apply {
                            total.postValue(t.data!!.total)
                            callback(this)
                        }
                    }

                    override fun onError(ex: ApiException) {
                        networkState.postValue(NetworkState.error(ex.getMessage()))
                    }


                })
    }

    override fun loadAfter(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, DriveListInfo>) {
        ApiClient.getInstance().getDriveList(params.key, state)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<DriveListParam>>() {
                    override fun onSuccess(t: BaseResp<DriveListParam>) {
                        t.data?.rows?.apply {
                            callback.onResult(this, params.key + 1)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }
                })
    }

    override fun loadBefore(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, DriveListInfo>) {
        ApiClient.getInstance().getDriveList(params.key,state)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<DriveListParam>>() {
                    override fun onSuccess(t: BaseResp<DriveListParam>){
                        t.data?.rows?.apply {
                            callback.onResult(this, params.key - 1)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }
                })
    }

    fun driveend(reserveid:Int,driveuid:Int,moth: () -> Unit, fail:()->Unit){
        ApiClient.getInstance().driveend(reserveid,driveuid)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Any>>() {
                    override fun onSuccess(t: BaseResp<Any>){
                       moth()
                    }

                    override fun onError(ex: ApiException) {
                        fail()
                    }
                })
    }

    fun cancelDrive(reserveid:Int,moth: () -> Unit, fail:()->Unit){
        ApiClient.getInstance().cancelDrive(reserveid)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Any>>() {
                    override fun onSuccess(t: BaseResp<Any>){
                        moth()
                    }

                    override fun onError(ex: ApiException) {
                        fail()
                    }
                })
    }

    fun getDriveDetail(reserveid:Int,moth: (info:DriveListInfo) -> Unit, fail:()->Unit){
        ApiClient.getInstance().getDriveDetail(reserveid)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<DriveListInfo>>() {
                    override fun onSuccess(t: BaseResp<DriveListInfo>){
                        t.data?.apply(moth)
                    }

                    override fun onError(ex: ApiException) {
                        fail()
                    }
                })
    }

    fun print(reserveid:Int, moth: (clueDetails: Reservemit) -> Unit, fail:()->Unit) {
        ApiClient.getInstance().printpdf(reserveid)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Reservemit>>() {
                    override fun onSuccess(t: BaseResp<Reservemit>) {
                        t.data?.apply(moth)
                    }
                    override fun onError(ex: ApiException) {
                        fail()
                    }
                })
    }

    /**
     *  客流接待-接待跟进
     */
    fun getfollow(id: Int, moth: (it: FollowParam) -> Unit) {
        ApiClient.getInstance().getfollow(id)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<FollowParam>>() {
                    override fun onSuccess(t: BaseResp<FollowParam>) {
                        t.data?.apply {
                            this.msg = t.msg ?: ""
                            moth(this)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }


                })
    }




}